package com.smartao.admin;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.stereotype.Controller;

import com.smartao.base.menu.Menu;
import com.smartao.base.menu.MenuService;
import com.smartao.base.menu.MenuType;
import com.smartao.core.info.ServerInfo;
import com.smartao.core.mvc.BaseAction;

@Controller
@Scope("prototype")
@Results({@Result(name = BaseAction.SUCCESS, location="main.jsp"),
	@Result(name = BaseAction.VIEW, location="sysinfo.jsp"),
	@Result(name = "viewOnlineUser", location="onlineUsers.jsp")})
public class MainAction extends BaseAction{
	
	private static final long serialVersionUID = 7879136748520364401L;
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private SessionRegistry sessionRegistry;
	
	private List<Menu> menus;
	
	private ServerInfo serverInfo;
	
	private List<Object> onlineUsers;
	
	@Override
	public String execute() throws Exception {
		menus = menuService.findSubMenuByMenutype(null, MenuType.admin);
        return SUCCESS;
    }
	
	public String sysinfo() throws Exception {
		serverInfo = new ServerInfo();
		return VIEW;
	}
	
	public String onlineUsers() throws Exception {
		onlineUsers = sessionRegistry.getAllPrincipals();
		return "viewOnlineUser";
	}

	public List<Menu> getMenus() {
		return menus;
	}

	public ServerInfo getServerInfo() {
		return serverInfo;
	}

	public List<Object> getOnlineUsers() {
		return onlineUsers;
	}

}
